package cate.game.play.buff.h;

import cate.common.table.d.GDSkill;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import easy.java.struct.ref.DoubleRef;

/**
 * 处于该状态的目标受击时消耗1层【真理守护】并抵消最多25%最大生命值的伤害,并将抵消伤害的50%恢复自身和1名随机友方单位的生命值
 */
public class 真理守护BH extends BuffHandler {


	private int 次数;

	private int 展示buff;

	private double 最大系数;

	private double 恢复系数;


	//次数=2&展示buff=？？&最大系数=2500&恢复系数=5000
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		次数 = args.getInt("次数", 0);
		展示buff = args.getInt("展示buff", 0);
		最大系数 = args.getWDouble("最大系数", 0d);
		恢复系数 = args.getWDouble("恢复系数", 0d);
	}

	private int num;

	@Override
	public void onAdd(ActionCtx action) {
		buff.owner.buff.tryAddSome(action, buff.owner, 展示buff,次数);
	}

	@Override
	public void adjustSkillDamage(ActionCtx action, Fighter actor, int reason, DoubleRef doubleRef) {
		num++;
		buff.owner.buff.removeBuffByTid(action, 展示buff, 1);
		double reduce = doubleRef.v;
		double max = buff.owner.attr.total().hpTotal() * 最大系数;
		reduce = Math.min(reduce, max);
		doubleRef.v -= reduce;

		double heal = reduce * 恢复系数;

		buff.owner.attr.buffHeal(action, buff.owner, heal);
		Fighter one = TargetSelector.findOne(buff.owner, action.getTeamVs(), new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS));
		if (one != null) {
			one.attr.buffHeal(action, buff.owner, heal);
		}
		if (num >= 次数) {
			buff.markInvalid(action);
		}
	}

	@Override
	public void onInvalid(ActionCtx action, byte invalidReason) {
		if (num > 0) {
			buff.owner.buff.removeBuffByTid(action, 展示buff, num);
		}
	}
}
